package com.huishou.lajiMain.controller;

import com.huishou.lajiMain.config.OssConfig;
import com.huishou.lajiMain.config.zhujie.AutoCustomAnnotation;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;

/*
 * @Author:大力王
 * @Date:2024/5/11-11-9:03
 * 开始了
 */
@RestController
@RequestMapping("/GarbageCollection")
@Api(tags = "垃圾回收接口")
@Slf4j
public class GarbageCollectionController {
    @Resource
    private OssConfig ossConfig;
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/collection")
    @AutoCustomAnnotation(ttl = 60 * 60 * 1000)
    public String garbageCollection(String vs) {
        //------------------------------写一个循环来解决redis有哪些key 开始-----------------------------------------
        // 计算过期时间
        long expirationTimeMillis =(60 * 60 * 1000);
        // 获取所有Redis键
        Set<String> keys = redisTemplate.keys("*");
        if (keys != null){
            for (String keyList : keys) {
                Long ttls = redisTemplate.getExpire(keyList);
                if (ttls != null && ttls != -1) { // 如果键设置了过期时间
                    if (ttls <= expirationTimeMillis) {
                        redisTemplate.delete(keyList);
                        ossConfig.clearExpiredObjects();
                        log.info("Deleted expired Redis key: " + keyList);
                    }
                }
            }
        }else {
            return "key删完了或没有key";
        }
        log.info(vs);
        log.info(redisTemplate.opsForValue().get("keygarbageCollection:rs")+"");
        return "你好！我是小高";
    }

    @PostMapping("/select")
    public String select() {
        log.info(redisTemplate.opsForValue().get("keygarbageCollection:[rs]")+"");
        return "";
    }
}
